package com.blue.waffle.iwc.screens;

import java.sql.SQLException;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.blue.waffle.iwc.domains.Profile;
import com.blue.waffle.iwc.game.IWCGame;
import com.blue.waffle.iwc.screens.ProfileScreen.ProfileButtonListener.ProfileButton;
import com.blue.waffle.iwc.ui.ButtonActorListener;
import com.blue.waffle.iwc.ui.CustomTextInputDialog;
import com.blue.waffle.iwc.ui.MessageDialog;

public class ProfileScreen extends AbstractScreen {

	private Window window;
	//private ScrollPane contentScrollPane;
	private Table contentTable;
	public static final int MAX_PROFILE=5;
	private static final float WINDOW_PADDINGS = 60;
	private static final float CELL_PADDINGS = 5;
	//private static final float ROWS = 4;
	//private static final int COLS = 4;
	private TextField nameTextField;
	
	private CustomTextInputDialog simpleTextInputDialog;
	
	public ProfileScreen(IWCGame game) {
		super(game);
	}
	
	@Override
	public void show() {
		super.show();
		Skin skin=getUiSkin();
		
		window=new Window("Create Profile", skin);
		gameStage.addActor(window);
		
		float windowWidth=IWCGame.VIRTUAL_WIDTH/5*2;
		window.setMovable(false);
		window.setBounds(windowWidth, WINDOW_PADDINGS,
				IWCGame.VIRTUAL_WIDTH-WINDOW_PADDINGS-windowWidth,
				IWCGame.VIRTUAL_HEIGHT - WINDOW_PADDINGS * 2);
		window.defaults().fill().padBottom(CELL_PADDINGS);
		
		setupContentTable(skin);
		window.row();
		window.add(contentTable).size(window.getWidth()-CELL_PADDINGS*2, 300).uniform();
		gameStage.addActor(getButtonTable(skin,new ProfileButtonListener(this, ProfileButton.BACK)).right().bottom());
		//window.add(getButtonTable(skin));
		
		simpleTextInputDialog.show();
	}
	
	private void setupContentTable(Skin skin) {
		Gdx.input.setOnscreenKeyboardVisible(false);
		contentTable=new Table(skin);
		nameTextField=new TextField("", skin);
		nameTextField.addListener(new ActorGestureListener(){
			@Override
			public void touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchDown(event, x, y, pointer, button);
				simpleTextInputDialog.setInitialText(((TextField)event.getTarget()).getText());
				simpleTextInputDialog.show();
			}
		});
		simpleTextInputDialog=new CustomTextInputDialog(nameTextField, "Enter your name", null);
		contentTable.row();
		contentTable.add("Enter a name:");
		contentTable.row();
		contentTable.add(nameTextField).width(300).uniform();
		contentTable.row();
		TextButton createButton=new TextButton("Create", skin);
		createButton.addListener(new ProfileButtonListener(this, ProfileButton.CREATE, nameTextField));
		contentTable.add(createButton).width(150).uniform();
		
	}
	
	public static class ProfileButtonListener extends ButtonActorListener{

		private ProfileButton button;
		private Object dependentObject;
		
		private static MessageDialog messageDialog;
		
		public ProfileButtonListener(AbstractScreen screen,ProfileButton button) {
			super(screen);
			this.button=button;
			/*if (messageDialog==null) {
				messageDialog=new MessageDialog("Name cannot be empty!", "Error", screen.getUiSkin());
			}*/
		}
		
		public ProfileButtonListener(AbstractScreen screen,ProfileButton button,Object dependentObject) {
			this(screen,button);
			this.dependentObject=dependentObject;
		}
		
		public enum ProfileButton {CREATE,BACK}
		
		private void createNewProfile(String name) throws SQLException{
			Profile profile=new Profile();
			profile.setName(name);
			profile.setLevelRandomSeed(System.currentTimeMillis());
			//profile.setLastPlayedLevel(0);
			screen.game.getGameManager().setUserProfile(profile).PersistProfile();
		}
		
		private void showProfileOverwriteDialog(Profile updateProfile){

			Skin skin=screen.getUiSkin();
			
			messageDialog=new MessageDialog("Create new profile will overwrite your existing profile.\nAre you sure to proceed?", "Overwrite Profile", skin, MessageDialog.BTN_YES_NO, new ButtonActorListener(screen,updateProfile){
				@Override
				public void clicked(InputEvent event,
						float x, float y) {
					
					super.clicked(event, x, y);
					String text=((Label)event.getTarget()).getText().toString();
					IWCGame game=screen.getGame();
					if (text.compareTo("Yes")==0) {
						try {
							Profile profile=(Profile)getData();
							game.getGameManager().setUserProfile(profile).PersistProfile();
							//game.getDatabaseHelper().getProfileLevelHelper().delete(profile);
							game.setScreen(new DifficultyScreen(game));
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}else {
						game.setScreen(new MenuScreen(game));
					}
				}
			});
			messageDialog.show(screen.gameStage);
		}
		
		private void handleCreate(){
			
			if (dependentObject instanceof TextField) {
				IWCGame game=screen.getGame();
				TextField nameTextField=(TextField)dependentObject;
				String name=nameTextField.getText();
				if (name!=null && name.trim().length()>0) {
					
					try {
						List<Profile> profiles=game.getDatabaseHelper().getProfileHelper().selectAll();
						if (profiles!=null && profiles.size()>0) {
							Profile profile=game.getGameManager().getUserProfile();
							profile.setName(name);
							profile.setLevelRandomSeed(System.currentTimeMillis());
							profile.setLastPlayedLevel(0);
							game.getGameManager().setUserProfile(profile).PersistProfile();
							showProfileOverwriteDialog(profile);
						}else {
							createNewProfile(name);
							game.setScreen(new DifficultyScreen(game));
						}
						
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				else {
					Skin skin=screen.getUiSkin();
					messageDialog=new MessageDialog("Name cannot be empty!", "Error", skin);
					//messageDialog.setupDialog();
					messageDialog.show(screen.gameStage);
				}
			}
		}
		
		@Override
		public void clicked(InputEvent event, float x, float y) {
			super.clicked(event, x, y);
			IWCGame game=screen.getGame();
			switch (button) {
			case CREATE:
				handleCreate();
				break;
			case BACK:
				game.setScreen(new MenuScreen(game));
				break;
			default:
				break;
			}
		}
		
	}

}
